package response

import (
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/bson/primitive"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

// entity.go 响应相关Mongo对象

// RespCode 响应码对象
type RespCode struct {
	ID         primitive.ObjectID `json:"id" bson:"_id"`
	PlatType   string             `json:"platType" bson:"platType"` // Type+Code 唯一确认一个应答消息
	Code       string             `json:"code" bson:"code"`
	DefaultMsg string             `json:"defaultMsg" bson:"defaultMsg"` //当语种没有匹配到时使用
	// 多语种
	Language map[string]string `json:"language" bson:"language"` //key 为语种  value 为值
}

func (r RespCode) GetIndex() []mongo.IndexModel {
	return []mongo.IndexModel{
		{
			Keys:    bson.D{{"platType", 1}, {"code", 1}},
			Options: options.Index().SetUnique(true),
		},
	}
}

func (r RespCode) CollectionName() string {
	return "resp_code"
}
func (r RespCode) ToRes(language string) Response {
	res := Response{
		Code:    r.Code,
		Message: r.DefaultMsg,
	}
	if r.Language == nil || language == "" || r.Language[language] == "" {
		return res
	}
	res.Message = r.Language[language]
	return res
}
